package com.yyx.lgb.manager.controller;


import com.yyx.lgb.manager.service.SysMenuService;
import com.yyx.lgb.manager.service.SysUserService;
import com.yyx.lgb.manager.service.ValidateCodeService;
import com.yyx.lgb.model.dto.system.LoginDto;
import com.yyx.lgb.model.entity.system.SysUser;
import com.yyx.lgb.model.vo.common.Result;
import com.yyx.lgb.model.vo.common.ResultCodeEnum;
import com.yyx.lgb.model.vo.system.LoginVo;
import com.yyx.lgb.model.vo.system.SysMenuVo;
import com.yyx.lgb.model.vo.system.ValidateCodeVo;
import com.yyx.lgb.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo =  sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
     ValidateCodeVo vo=   validateCodeService.generateValidateCode();
     return Result.build(vo,ResultCodeEnum.SUCCESS);
    }
 /*   @Operation(summary = "获取当前登录用户信息")
    @GetMapping(value = "/getUserinfo")
    public Result<System> GetUserinfo(@RequestHeader(name="token") String token){
         SysUser sysUser= sysUserService.getUserinfo(token);
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }*/
 @Operation(summary = "获取当前登录用户信息")
 @GetMapping(value = "/getUserinfo")
 public Result<System> GetUserinfo(){
     return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
 }
    @Operation(summary = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

//查询用户可以操作的菜单
    @GetMapping("/menus")
    public Result menus(){
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }

}
